package be.sbw.wave.extensions.publisher.model.junit;


import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.google.wave.api.Annotation;
import com.google.wave.api.Range;

import be.sbw.wave.extensions.publisher.model.TextViewItem;

public class TextView {

	private String text;
	private List<Annotation> annotations;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}
	
	@Before
	public void setUp(){
		this.text="Test\ntest\nUpdate\nIn color RED\nor \nblue\nor Big blue\nre-test";
		annotations = new ArrayList<Annotation>();
		annotations.add(new Annotation("conv/title", "", new Range(-1,4)));
		annotations.add(new Annotation("style/textDecoration", "underline", new Range(5,9)));
		annotations.add(new Annotation("style/backgroundColor", "rgb(0, 213, 255)", new Range(7,11)));
		annotations.add(new Annotation("style/fontFamily", "monospace", new Range(10,16)));
		annotations.add(new Annotation("style/color", "rgb(0, 153, 0", new Range(13,14)));
		annotations.add(new Annotation("lang", "en", new Range(16,29)));
		annotations.add(new Annotation("style/color", "rgb(229, 51, 51)", new Range(17,30)));
		annotations.add(new Annotation("style/color", "rgb(76, 51, 229)", new Range(30,58)));
		annotations.add(new Annotation("lang", "en", new Range(38,50)));
		annotations.add(new Annotation("style/fontSize", "2.3333333333333335em", new Range(39,51)));
		annotations.add(new Annotation("style/fontSize", "1.1666666666666667em", new Range(51,58)));
		annotations.add(new Annotation("style/fontFamily", "sans-serif", new Range(51,58)));
	}
	
	@Test
	public void getHtml(){
		String renderedHtml = "";
		// create the list
		List<TextViewItem> textItems = new ArrayList<TextViewItem>();
		for(int i=0;i<text.length();i++){
			textItems.add(new TextViewItem(i,text.charAt(i)));
		}
		// loop through annotation to flag each list litem
		for(Annotation annotation:annotations){
			int rangeFrom = annotation.getRange().getStart();
			if(rangeFrom<0){rangeFrom=0;}
			int rangeTo = annotation.getRange().getEnd();
			if(rangeTo>text.length()){rangeTo=text.length();}
			for(int i=rangeFrom;i<rangeTo;i++){
				textItems.get(i).Style.put(annotation.getName(), annotation.getValue());
			}
		}
		// parse the list to create the appropriate html output
		String previousStyle="";
		int openedTags=0;
		for(int i=0;i<textItems.size();i++){
			TextViewItem current = textItems.get(i);
			String currentConcatenatedStyle=current.getConcatenatedStyle();
			if(!currentConcatenatedStyle.equals(previousStyle)){
				previousStyle=currentConcatenatedStyle;
				if(openedTags>0){
					renderedHtml+="</span>";
					openedTags--;
				}
				String htmlStyle = current.getHtmlStyle();
				if(htmlStyle.length()>0){
					renderedHtml+="<span style=\""+htmlStyle+"\">";
					openedTags++;
				}
			}
			renderedHtml+=textItems.get(i).Text;
		}
		for(int i=0;i<openedTags;i++){
			renderedHtml+="</span>";
		}
		renderedHtml=renderedHtml.replace("\n", "<br/>\n");
		assertEquals("Test<br/>\n" +
				"<span style=\"text-decoration:underline;\">te</span><span style=\"text-decoration:underline;background-color:rgb(0, 213, 255);\">st</span><span style=\"background-color:rgb(0, 213, 255);\"><br/>\n" +
				"</span><span style=\"background-color:rgb(0, 213, 255);font-family:monospace;\">U</span><span style=\"font-family:monospace;\">pd</span><span style=\"color:rgb(0, 153, 0;font-family:monospace;\">a</span><span style=\"font-family:monospace;\">te</span><br/>\n" +
				"<span style=\"color:rgb(229, 51, 51);\">In color RED</span><span style=\"color:rgb(229, 51, 51);\"><br/>\n" +
				"</span><span style=\"color:rgb(76, 51, 229);\">or <br/>\n" +
				"blue</span><span style=\"color:rgb(76, 51, 229);\"><br/>\n" +
				"</span><span style=\"font-size:2.3333333333333335em;color:rgb(76, 51, 229);\">or Big blue</span><span style=\"font-size:2.3333333333333335em;color:rgb(76, 51, 229);\"><br/>\n" +
				"</span><span style=\"font-size:1.1666666666666667em;color:rgb(76, 51, 229);font-family:sans-serif;\">re-test</span>", renderedHtml);
	}

}
